﻿using BaseDll;
using Emgu.CV.ML;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using UIDATALayer;
namespace UserData
{
    public class TrayReflushData : ShowData
    {
    }
    //料盘方法
    public class TrayData
    {
        public string currentFile = AppDomain.CurrentDomain.BaseDirectory;

        public TrayData()
        {
           
        }
        public TrayData Read(string name)
        {
            currentFile += $"\\config\\TrayData\\{name}.json";
            if (!File.Exists(currentFile))
            {
                Save();
            }
            Object obs = AccessJosnSerializer.JsonToObject(currentFile, typeof(TrayData));
            if (obs != null)
            {
               return (TrayData)obs;
            }
            return null;
        }
        private void Save()
        {
            AccessJosnSerializer.ObjectToJson(currentFile, this);
            return;

        }
        public delegate void EventTrayDataChange();
        public event EventTrayDataChange OnTestTrayDataChange = null;
        private Coordinate Snap_左上 = new Coordinate();
        private Coordinate Snap_右上 = new Coordinate();
        private Coordinate Snap_右下 = new Coordinate();
        private Coordinate Pick_左上 = new Coordinate();
        private Coordinate Pick_右上 = new Coordinate();
        private Coordinate Pick_右下 = new Coordinate();
        public int RowCount = 0;
        public int ColCount = 0;
        public int RowNow = 0;
        public int ColNow = 0;
        public List<bool> list_TrayCellResult = new List<bool>();
        public List<string> list_TrayCellName = new List<string>();

        public void LoadTrayData(Coordinate coord_Snap左上, Coordinate coord_Snap右上, Coordinate coord_Snap右下, 
                                 Coordinate coord_Pick左上, Coordinate coord_Pick右上, Coordinate coord_Pick右下, int n_Row, int n_Column)
        {
            Pick_左上 = coord_Pick左上;
            Pick_右上 = coord_Pick右上;
            Pick_右下 = coord_Pick右下;
            Snap_左上 = coord_Snap左上;
            Snap_右上 = coord_Snap右上;
            Snap_右下 = coord_Snap右下;
            RowCount = n_Row;
            ColCount = n_Column;

            if (list_TrayCellResult.Count !=(n_Row* n_Column))
            {
                list_TrayCellResult = new List<bool>();
                list_TrayCellName= new List<string>();
                for (int i = 0; i < n_Row; i++)
                {
                    for (int j = 0; j < n_Column; j++)
                    {
                        list_TrayCellResult.Add(true);
                        list_TrayCellName.Add((j + i * n_Column + 1).ToString());
                    }
                }
            }
            if (OnTestTrayDataChange != null)
                OnTestTrayDataChange();
            Save();
        }

        public void InitData()
        {
            RowNow = 0;
            ColNow = 0;
            list_TrayCellResult = new List<bool>();
            list_TrayCellName = new List<string>();
            for (int i = 0; i < RowCount; i++)
            {
                for (int j = 0; j < ColCount; j++)
                {
                    list_TrayCellResult.Add(true);
                    list_TrayCellName.Add((j + i * ColCount + 1).ToString());
                }
            }
            if (OnTestTrayDataChange != null)
                OnTestTrayDataChange();
        }

        public Coordinate GetSpecialCoordinate(int n_Row, int n_Column, bool b_SnapPos = false)
        {
            if (n_Row > RowCount || n_Column > ColCount)
                throw new Exception("行数或者列数不对");
            Point2d Point_StepRow = new Point2d();
            Point2d Point_StepColumn = new Point2d();
            Coordinate point_Begin = new Coordinate();
            if (b_SnapPos)
            {
                if (RowCount <= 1)
                    Point_StepRow = new Point2d();
                else
                {
                    Point_StepRow.x = (Snap_右下.X - Snap_右上.X) / (RowCount - 1);
                    Point_StepRow.y = (Snap_右下.Y - Snap_右上.Y) / (RowCount - 1);
                }
                if (ColCount <= 1)
                    Point_StepColumn = new Point2d();
                else
                {
                    Point_StepColumn.x = (Snap_右上.X - Snap_左上.X) / (ColCount - 1);
                    Point_StepColumn.y = (Snap_右上.Y - Snap_左上.Y) / (ColCount - 1);
                }
                point_Begin = Snap_左上;
            }
            else
            {
                if (RowCount <= 1)
                    Point_StepRow = new Point2d();
                else
                {
                    Point_StepRow.x = (Pick_右下.X - Pick_右上.X) / (RowCount - 1);
                    Point_StepRow.y = (Pick_右下.Y - Pick_右上.Y) / (RowCount - 1);
                }
                if (ColCount <= 1)
                    Point_StepColumn = new Point2d();
                else
                {
                    Point_StepColumn.x = (Pick_右上.X - Pick_左上.X) / (ColCount - 1);
                    Point_StepColumn.y = (Pick_右上.Y - Pick_左上.Y) / (ColCount - 1);
                }
                point_Begin = Pick_左上;
            }
            Coordinate point_Result = point_Begin;
            point_Result.X = point_Begin.X + n_Row * Point_StepRow.x + n_Column * Point_StepColumn.x;
            point_Result.Y = point_Begin.Y + n_Row * Point_StepRow.y + n_Column * Point_StepColumn.y;
            return point_Result;
        }
        public Coordinate GetSpecialCoordinate(bool b_SnapPos = false)
        {
            return GetSpecialCoordinate(RowNow, ColNow, b_SnapPos);
        }      
        public bool ChangeToNext()
        {
            if (ColNow < ColCount - 1)
            {
                ColNow++;
            }
            else
            {
                if (RowNow < RowCount - 1)
                {
                    ColNow = 0;
                    RowNow++;
                }
                else
                {
                    return false;
                }
                
            }
            if (OnTestTrayDataChange != null)
                OnTestTrayDataChange();
            Save();
            return true;
        }
        public void ChangeTrayCellToNG(string strMsg)
        {
            list_TrayCellResult[RowNow * ColNow + ColNow] = false;
            list_TrayCellName[RowNow * ColNow + ColNow] = strMsg;
            if (OnTestTrayDataChange != null)
                OnTestTrayDataChange();
            Save();
        }
        public void SetRowAndColumn(int n_Row, int n_Column,bool b_Result=true)
        {
            if (n_Row > RowCount || n_Column > ColCount)
                throw new Exception("行数或者列数不对");
            RowNow = n_Row;
            ColNow = n_Column;
            if (OnTestTrayDataChange != null)
                OnTestTrayDataChange();
            Save();
        }
    }
    //料盘数据可无限添加,可保存当前行列数
    public class TrayDataMgr
    {
        public Dictionary<string, TrayData> dicTrayData = new Dictionary<string, TrayData>();
        private static TrayDataMgr dataMgr = null;
        private static object obj = new object();

        public static TrayDataMgr GetInstance()
        {
            if (dataMgr == null)
            {
                lock (obj)
                {
                    dataMgr = new TrayDataMgr();
                    return dataMgr;
                }

            }
            else
                return dataMgr;
        }
        public void AddTrayData(string strTrayName)
        {
            if(!dicTrayData.ContainsKey(strTrayName))
            {
                TrayData data = new TrayData();
                data = data.Read(strTrayName);
                dicTrayData.Add(strTrayName, data);
            }
            
        }


 

    }
}
